Skip to main content

Introduction to objects and Java

Object Oriented Paradigm - Way you think about code and programming. Libraries for mostly everything. Think it as interacting objects which have a state and can do things

Benefits of OO

Improved understanding of program structure

  • Logical structure, reflecting real world
  • Especially large programs, give useful decomposition

Can easily reuse sections of code, and ease of change Can debug code easily, enforce constraints, and easier to validate changes.

  • Modular nature, and encapsulation help to localise problems

Things will be in memory and will be able to track itself. Objects have limits on what can change the data, only certain things can alter it

What is an OOL

Language which promotes or allows object oriented programming and design:

  • Objects (and classes)
  • Encapsulation (methods and data together)
  • Data hiding (restricts access to data)
  • Composition (stronger than aggregation)
  • Inheritance (reuse)
  • Abstraction (and interfaces)
  • Polymorphism (dynamic dispatch) Classes are 'plans' for an objects

Better support for OO programming

  • Enforcing OO behaviour
  • Fewer lines of code to get )) behaviour
  • Explicit syntax: more obvious what code is doing

Java

Compiled to an intermediate 'byte code' which is executed on target devices. Resulting .class file can be run on any platform inside a 'virtual machine' (JVM)

Advantages

  • 'Run anywhere'
  • Many libraries of reusable classes
  • Focus on language and library design is often on good OOP rather than speed
  • Reuse of classes is important, speeds up development and speeds debugging

Don't need to worry about pointers

Objects and Classes

C structs can be considered to be very simple objects Class - Blueprint for creating an object Object - Some collection of data that exists at runtime which can apply functions to Can have many different 'instances' of a class. Types of things are still classes not objects - they are still designs rather than a specific style. With an object should be able to say this one

Classes specify what is in objects of that type Objects are instances of classes

public - anything can access it private - only the class members can access it

All executable code in Java must be in classes Functions = methods